package cn.itsource.pethome.user.service.impl;

import cn.itsource.pethome.Basic.service.Impl.BaseServiceImpl;
import cn.itsource.pethome.Basic.util.AjaxResult;
import cn.itsource.pethome.Basic.util.RedisUtils;
import cn.itsource.pethome.Basic.util.StrUtils;
import cn.itsource.pethome.user.constant.PhoneConstant;
import cn.itsource.pethome.user.domain.User;
import cn.itsource.pethome.user.domain.dto.UserDto;
import cn.itsource.pethome.user.mapper.UserMapper;
import cn.itsource.pethome.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public AjaxResult checkPhone(String phone) {
            //通过电话号码查询用户
            User user = userMapper.checkPhone(phone);
            if(user==null){
                return new AjaxResult();
            }else {
                return new AjaxResult(false,"您已被注册过!!!!!!!");
            }
    }

    /**
     * 数据验证
     * @param userDto
     */
    public void checkUserDto(UserDto userDto){
        if(StringUtils.isEmpty(userDto.getPhone())||StringUtils.isEmpty(userDto.getCode())||StringUtils.isEmpty(userDto.getPassword())||StringUtils.isEmpty(userDto.getConfirmPassword())){
            throw new RuntimeException("数据不能为空!!!!");
        }
        if(!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new RuntimeException("两次密码不一致!!!");
        }
        //从redis获取验证码
        String s = RedisUtils.INSTANCE.get(PhoneConstant.PHONE_REG + ":" + userDto.getPhone());
        if(StringUtils.isEmpty(s)){
            throw new RuntimeException("验证码已过期，请重新发送!!!!");
        }
        //截取验证码
        String code = s.split(":")[0];
        if (!code.toUpperCase().equals(userDto.getCode().toUpperCase())){
            throw new RuntimeException("验证码错误");
        }
    }

    /**
     * 将后端数据转换为User
     * @param userDto
     * @return
     */
    public User UserDtoToUser(UserDto userDto) {
        //验证前端数据
        checkUserDto(userDto);
        //将前端数据源UserDto转为User
        User user = new User();
        BeanUtils.copyProperties(userDto,user);
        user.setUsername(userDto.getPhone());
        //获取随机盐值
        String randomString = StrUtils.getComplexRandomString(10);
        //设置密码
        user.setPassword(userDto.getPassword()+randomString);
        //设置盐值
        user.setSalt(randomString);
        //设置状态
        user.setState(PhoneConstant.ACTIVE);
        return user;
    }

    @Override
    public void register(UserDto userDto) {
        //数据验证
        checkUserDto(userDto);
        //转换数据
        User user = UserDtoToUser(userDto);
        //保存数据
        userMapper.save(user);

    }

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User login(String username, String password) {
        //通过用户名查询
        User user = userMapper.checkPhone(username);
        if (user == null) {
            throw new RuntimeException("账户不能为空!!!!!");
        }
        if(!user.getPassword().equals(password+user.getSalt())){
            throw new RuntimeException("密码错误");
        }
        return user;
    }
}
